
<template>
  <div class="login-info">
    <el-row>
      <el-col :xs="24" :sm="11" :md="8" :lg="9" :xl="8">
        <h3 style="text-align: center;">登录界面</h3>
        <el-form :model="ruleForm"  :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
           <el-form-item label="用户名：" prop="username">
            <el-input  v-model="ruleForm.username" autocomplete="off" clearable>
            </el-input>
          </el-form-item>
          <el-form-item label="密码：" prop="password">
            <el-input type="password" v-model="ruleForm.password" autocomplete="off" show-password clearable></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>
 
<script>
import {login} from '../api/users'
  export default {
    name: 'login',
    data() {
      var validateAccount = (rule,value,callback)=>{
        if(value ===''){
          return callback(new Error("账号不能为空"));
        }else{
          callback();
        }
      };
      var validatePassword = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          callback();
        }
      };
      return {
        ruleForm: {
          username: '',
          password: '',
        },
        rules: {
          username: [{
            validator: validateAccount,
            trigger: 'blur'
          }],
          password: [{
            validator: validatePassword,
            trigger: 'blur'
          }]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            login(this.ruleForm).then(response => {
                console.log(response.data);
              if(response.code==1000){
                localStorage.setItem("accessToken", response.data.token);
                localStorage.setItem("refreshToken", response.data.refreshToken);
          this.$message({
          message: response.data.msg,
         
          type: 'success'
        });

         this.$router.push('/')
            
            }else{

            this.$message.error({message: response.data.msg});
              return false;
            }
            });
            
          } else {
            this.$message.error('登录失败');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>
 
<style scoped>
.login-info >>> .el-col{
    background-color: #e5e8ec;
    padding: 2% 5% 0% 2%;
}
/* .login-info >>> .el-form-item{
  width:63%;
} */
 
</style>

